const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

const Product = sequelize.define('Product', {
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    name: {
        type: DataTypes.STRING(200),
        allowNull: false,
        validate: {
            notEmpty: {
                msg: '商品名称不能为空'
            },
            len: {
                args: [2, 200],
                msg: '商品名称长度必须在2-200个字符之间'
            }
        }
    },
    description: {
        type: DataTypes.TEXT,
        allowNull: true
    },
    shortDescription: {
        type: DataTypes.STRING(500),
        allowNull: true
    },
    sku: {
        type: DataTypes.STRING(100),
        allowNull: false,
        unique: {
            msg: 'SKU 已存在'
        },
        validate: {
            notEmpty: {
                msg: 'SKU 不能为空'
            }
        }
    },
    price: {
        type: DataTypes.DECIMAL(10, 2),
        allowNull: false,
        validate: {
            min: {
                args: 0,
                msg: '价格不能为负数'
            }
        }
    },
    originalPrice: {
        type: DataTypes.DECIMAL(10, 2),
        allowNull: true,
        validate: {
            min: {
                args: 0,
                msg: '原价不能为负数'
            }
        }
    },
    stock: {
        type: DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 0,
        validate: {
            min: {
                args: 0,
                msg: '库存不能为负数'
            }
        }
    },
    lowStockThreshold: {
        type: DataTypes.INTEGER,
        defaultValue: 10,
        validate: {
            min: {
                args: 0,
                msg: '库存预警阈值不能为负数'
            }
        }
    },
    weight: {
        type: DataTypes.DECIMAL(8, 2),
        allowNull: true,
        validate: {
            min: {
                args: 0,
                msg: '重量不能为负数'
            }
        }
    },
    dimensions: {
        type: DataTypes.JSON,
        allowNull: true
    },
    images: {
        type: DataTypes.JSON,
        allowNull: true,
        defaultValue: []
    },
    tags: {
        type: DataTypes.JSON,
        allowNull: true,
        defaultValue: []
    },
    isActive: {
        type: DataTypes.BOOLEAN,
        defaultValue: true
    },
    isFeatured: {
        type: DataTypes.BOOLEAN,
        defaultValue: false
    },
    viewCount: {
        type: DataTypes.INTEGER,
        defaultValue: 0
    },
    salesCount: {
        type: DataTypes.INTEGER,
        defaultValue: 0
    },
    categoryId: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: {
            model: 'categories',
            key: 'id'
        }
    }
}, {
    tableName: 'products',
    timestamps: true,
    indexes: [
        {
            fields: ['categoryId']
        },
        {
            fields: ['sku']
        },
        {
            fields: ['isActive']
        },
        {
            fields: ['isFeatured']
        }
    ]
});

// 实例方法：检查是否有库存
Product.prototype.hasStock = function(quantity = 1) {
    return this.stock >= quantity;
};

// 实例方法：检查是否库存不足
Product.prototype.isLowStock = function() {
    return this.stock <= this.lowStockThreshold;
};

// 实例方法：获取折扣百分比
Product.prototype.getDiscountPercentage = function() {
    if (this.originalPrice && this.originalPrice > this.price) {
        return Math.round(((this.originalPrice - this.price) / this.originalPrice) * 100);
    }
    return 0;
};

module.exports = Product;